package guoanmaker.operator.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


import guoanmaker.operator.business.model.OperatorSendMesToCus;
import guoanmaker.operator.business.model.PersonalUser;
import guoanmaker.operator.business.model.PerviderDictionary;
import guoanmaker.operator.business.repository.OperatorSendMesToCusRepositoy;
import guoanmaker.operator.business.repository.PersonalUserRepository;
import guoanmaker.operator.business.repository.ProviderDictionaryRepository;

@Service
public class OperatorSendMesToCusService implements OperatorSendMesToCusServiceIntegerface {
	@Autowired
	private OperatorSendMesToCusRepositoy operatorSendMesToCusRepositoy;
	@Autowired
	private PersonalUserRepository personalUserRepository;
	@Autowired
	private  ProviderDictionaryRepository providerDictionaryRepository;
	@Transactional
	public OperatorSendMesToCus saveOperatorSendMesToCus(OperatorSendMesToCus operatorSendMesToCus){
		return operatorSendMesToCusRepositoy.save(operatorSendMesToCus);
		
		}
	
	 @Override
	    public Page<OperatorSendMesToCus> getOperatorSendMesToCus(Integer page, Integer size,String phone) {
	        return operatorSendMesToCusRepositoy.findSendMesToCusList(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "sendTime")),phone);
	    }

	 
	 @Override
	    public Map getOperatorSendMesToCusList(Integer page, Integer size,String phone,String sendStatus) {
		 Map<String,Object> remap=new HashMap<String,Object>();
		 int count=0;
		 Page<OperatorSendMesToCus> allpcListBypage=null;
		 List<OperatorSendMesToCus> allpcList=null;
		 if(null!=sendStatus&&!sendStatus.equals("")){
			 allpcListBypage= operatorSendMesToCusRepositoy.findSendMesToCusListBystatus(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "sendTime")), phone, sendStatus);
			 allpcList=operatorSendMesToCusRepositoy.findPcBystatus(phone,sendStatus);
		 }else{
			 allpcListBypage=operatorSendMesToCusRepositoy.findSendMesToCusListPc(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "sendTime")),phone);
			 allpcList=operatorSendMesToCusRepositoy.findAllPcList(phone);
		 }
		 if(allpcList.size()>0){
			 count=allpcList.size();
		 }
		 remap.put("allpcListBypage", allpcListBypage);
		 remap.put("count", count);
	        return remap;
	    }

	@Override
	@Transactional
	public OperatorSendMesToCus selectSendMesToCus(String id) {
		// TODO Auto-generated method stub
		 OperatorSendMesToCus sc=operatorSendMesToCusRepositoy.findOne(id);
		 sc.setSendStatus("2");
		 OperatorSendMesToCus ost= operatorSendMesToCusRepositoy.save(sc);
		 return ost;
	}

	@Override
	@Transactional
	public void deleteSendMesToCusById(String id) {
		// TODO Auto-generated method stub
		operatorSendMesToCusRepositoy.delete(id);
	}

	@Override
	public List<OperatorSendMesToCus> selectNoSendMesToCus(String phone,String sendStatus) {
		// TODO Auto-generated method stub
		return operatorSendMesToCusRepositoy.findPcBystatus(phone,sendStatus);
	}
/**
 * 订单完成发站内信抽奖
 */
	@Override
	@Transactional
	public void sendPayFor(String activeUrl,String userid,String orderNumber) {
		try{
		PersonalUser personalUser=personalUserRepository.findOne(userid);
		 OperatorSendMesToCus od=new OperatorSendMesToCus();
		 if(personalUser.getEmail()!=null){
		 od.setEmail(personalUser.getEmail());}
		 od.setPhone(personalUser.getUsername());
		 od.setSendWay("13");
		
		 od.setMesContent("恭喜您成功支付"+orderNumber+"订单，获得一次抽奖机会，点此条信息进行抽奖！"+"fadsvjihfandsvoihuibjwtengfdsviuobcv"+activeUrl);
		 od.setMesTitle("抽奖信息");
		 od.setSendTime(new Date());
		 od.setSendStatus("1");//1  未读  2已读
	 
		 od.setMesType("19");
		 operatorSendMesToCusRepositoy.save(od);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	

}